<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" v-if="props.search" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="userId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        border
        stripe
        :where="defaultWhere"
        :loadOnCreated="!props.search"
      >
        <template #toolbar="{}">
          <span class="mr8 ml8">总账单：{{ totalAmount }}元</span>
          <span>总欠款：{{ totalDebt }}元</span>
        </template>
        <template #realname="{ row }">
          <span class="blue pointer" @click="showStudentDetail(row)">
            {{ row.realname }}
          </span>
          <el-tag
            v-if="row.once_blacklist === 1"
            class="pointer"
            style="margin-left: 5px"
            size="small"
            type="info"
            @click="showBlackList(row)"
          >
            黑
          </el-tag>
        </template>
        <template #repiad="{ row }">
          <span class="warning pointer" @click="openBillRepayRecord(row)">
            {{ row.repiad }}
          </span>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 学生详情 -->
    <Detail v-model="showDetail" :student-id="currentId" @done="reload" />
    <Repay v-model="showBillRepayRecord" :bill-id="currentId" @done="reload" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import Search from './components/search.vue';
  import Repay from './components/repay.vue';
  import Detail from '@/views/student/components/detail.vue';
  import { pageBill } from '@/api/money';

  const props = defineProps({
    search: {
      type: Boolean,
      default: true
    },
    studentId: {
      type: Number,
      default: 0
    }
  });

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'id',
      label: 'ID',
      width: 100
    },
    {
      prop: 'tenant',
      label: '所属公司',
      width: 100
    },
    {
      slot: 'realname',
      label: '姓名',
      width: 100
    },
    {
      prop: 'amount',
      label: '金额',
      align: 'center',
      width: 100
    },
    {
      slot: 'repiad',
      label: '已还',
      align: 'center',
      width: 100
    },
    // {
    //   prop: 'repay_time',
    //   label: '到期还款日',
    //   width: 120,
    //   align: 'center'
    // },
    {
      prop: 'applyTime',
      label: '申请日期',
      width: 120,
      align: 'center'
    },
    {
      prop: 'judge_user',
      label: '审核人',
      width: 100
    },
    {
      prop: 'judge_time',
      label: '审核日期'
    }
    // {
    //   prop: 'paid_user',
    //   label: '打款人',
    //   width: 100
    // },
    // {
    //   prop: 'paid_time',
    //   label: '打款日期',
    //   width: 120
    // },
    // {
    //   prop: 'tel',
    //   label: '电话',
    //   width: 120,
    //   align: 'center'
    // }
    // {
    //   prop: 'school',
    //   label: '学校',
    //   minWidth: 200
    // }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const currentId = ref(null);

  /** 是否显示编辑弹窗 */
  const showDetail = ref(false);

  /** 表格数据源 */
  const datasource = async ({ page, where, orders, limit }) => {
    const result = await pageBill({
      ...where,
      ...orders,
      page,
      limit
    });
    totalAmount.value = result.totalAmount;
    totalDebt.value = result.totalDebt;
    return result;
  };
  const totalAmount = ref(0);
  const totalDebt = ref(0);

  const defaultWhere = ref({
    realname: '',
    tel: '',
    identifier: '',
    tenant_id: '',
    user_id: props.studentId,
    type: 'all'
  });

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 还款记录 */
  const showBillRepayRecord = ref(false);
  const openBillRepayRecord = (row) => {
    currentId.value = row.bill_id;
    showBillRepayRecord.value = true;
  };

  /** 打开编辑弹窗 */
  const showStudentDetail = (row) => {
    currentId.value = row.user_id ?? 0;
    showDetail.value = true;
  };
</script>

<script>
  export default {
    name: 'SystemUser'
  };
</script>
